package com.scanner.hardware;

import android.content.BroadcastReceiver;
import android.content.Context;
import android.content.Intent;
import android.content.IntentFilter;
import android.support.v7.widget.Toolbar;
import android.view.MenuItem;
import android.widget.Button;
import android.widget.TextView;
import android.widget.Toast;

import com.scanner.barcodeservice.ScannerPortService;
import com.scanner.base.BaseActivity;

import butterknife.BindView;

public class ScannerMainActivity extends BaseActivity {

    @BindView(R.id.main_toolbar)
    Toolbar mainToolbar;
    @BindView(R.id.tv_content)
    TextView tvContent;
    @BindView(R.id.btn_clear)
    Button btnClear;
    private int baudrate;

    @Override
    protected int getLayout() {
        return R.layout.activity_scanner_main;
    }

    @Override
    protected void initView() {
        mainToolbar.inflateMenu(R.menu.settingmenu);
        mainToolbar.setOnMenuItemClickListener(new Toolbar.OnMenuItemClickListener() {
            @Override
            public boolean onMenuItemClick(MenuItem item) {
                switch (item.getItemId()) {
                    case R.id.toolbar_setting:
                        Toast.makeText(ScannerMainActivity.this, "收藏", Toast.LENGTH_SHORT).show();
                        break;
                    case R.id.toolbar_symb:
                        Toast.makeText(ScannerMainActivity.this, "分享", Toast.LENGTH_SHORT).show();
                        break;
                    case R.id.toolbar_export:
                        Toast.makeText(ScannerMainActivity.this, "字号", Toast.LENGTH_SHORT).show();
                        break;
                    case R.id.toolbar_about:
                        Toast.makeText(ScannerMainActivity.this, "模式", Toast.LENGTH_SHORT).show();
                        break;
                }
                return true;
            }
        });
        Intent intent = getIntent();
        //获取波特率
        baudrate = intent.getIntExtra("baudrate", 115200);

        IntentFilter intentFilter = new IntentFilter();
        intentFilter.addAction(ScannerPortService.BARCODEPORT_RECEIVED_ACTION);
        registerReceiver(receiver, intentFilter);

        //开机时不自动启动该app，进入该app时启动
        Intent screenMonitorServiceIntent=new Intent(this,ScannerPortService.class);
        startService(screenMonitorServiceIntent);
    }

    /**
     * 广播接收器
     * */
    private BroadcastReceiver receiver = new BroadcastReceiver() {
        @Override
        public void onReceive(Context context, Intent intent) {
            // TODO Auto-generated method stub
            String action = intent.getAction();
            if(action!=null){
                if(action.equals(ScannerPortService.BARCODEPORT_RECEIVED_ACTION)){
                    String data=intent.getStringExtra(ScannerPortService.BARCODEPORT_RECEIVEDDATA_EXTRA_DATA);
                    if(data!=null){
                        tvContent.append(data);
                    }
                }
            }
        }
    };
}
